package nsu.edu.zsq.controller.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import nsu.edu.zsq.bean.SchClass;
import nsu.edu.zsq.bean.SchMajor;
import nsu.edu.zsq.bean.StuCourse;
import nsu.edu.zsq.common.JsonData;
import nsu.edu.zsq.exception.ParamException;
import nsu.edu.zsq.param.PageQuery;
import nsu.edu.zsq.param.TchCourseParam;
import nsu.edu.zsq.service.sys.CourseService;
import nsu.edu.zsq.service.sys.SchClassService;
import nsu.edu.zsq.service.sys.SchMajorService;
import nsu.edu.zsq.service.sys.StudentCourseService;

/** 
* Description: 学生课程的controller类<br>
*/
@RequestMapping("/sys/student/course")
@Controller
public class StudentCourseController {
    
    @Autowired
    private SchClassService schClassService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private SchMajorService schMajorService;
    @Autowired
    private StudentCourseService studentCourseService;
    
    /**
     * 获取院系+专业的tree结构数据
     * @return JsonData
     */
    @RequestMapping("/schOrgWithMajorTree")
    @ResponseBody
    public JsonData schOrgWithMajorTree() {
        String listSchOrgWithMajorTree = courseService.listSchOrgWithMajorTree();
        List<SchMajor> listSchMajor = schMajorService.listSchMajor();
        // 获取班级的课程数量
        List<Map<String, Integer>> countCourse = studentCourseService.countCourseByClass();
        Map<String, Object> map = new HashMap<String, Object>(3);
        map.put("listSchOrgWithMajorTree", listSchOrgWithMajorTree);
        map.put("listSchMajor", listSchMajor);
        map.put("countCourse", countCourse);
        return JsonData.success(map);
    }
    
    /**
     * 根据专业代码获取班级的分页数据
     * @param majorNo 专业代码
     * @param pageQuery 分页数据
     * @return JsonData
     */
    @RequestMapping("/listSchClass")
    @ResponseBody
    public JsonData listSchClassWithPage(@RequestParam("majorNo") String majorNo, PageQuery pageQuery) {
        PageInfo<SchClass> listSch = schClassService.listSchClassWithPageByMajorNo(majorNo, pageQuery);
        return JsonData.success(listSch);
    }
    
    // ============================================课程管理===============================================
    
    /**
     * 跳转到学生课程管理页面
     * @param majorNo 专业代码
     * @param classId 班级id
     * @param model
     * @return String manager/student/course/stuCourse_manager.html
     */
    @RequestMapping("/toStuCourseManager")
    public String toStuCourseManager(@RequestParam("majorNo")String majorNo, @RequestParam("classId") int classId, Model model) {
        model.addAttribute("majorNo", majorNo);
        model.addAttribute("classId", classId);
        return "manager/student/course/stuCourse_manager.html";
    }
    
    /**
     * 跳转到课程管理-添加课程页面
     * @param majorNo 专业代码
     * @param classId 班级id
     * @param model
     * @return String manager/student/course/teaCourse_manager_addCourse.html
     */
    @RequestMapping("/toManagerAddCourse")
    public String toManagerAddCourse(@RequestParam("majorNo")String majorNo, @RequestParam("classId") int classId, Model model) {
        model.addAttribute("majorNo", majorNo);
        model.addAttribute("classId", classId);
        return "manager/student/course/stuCourse_manager_addCourse.html";
    }
    
    /**
     * 根据班级id获取该班级所有教师课程
     * @param classId 班级id
     * @return JsonData
     */
    @RequestMapping("/listClassCourse")
    @ResponseBody
    public JsonData listClassCourse(@RequestParam("classId")int classId) {
        List<StuCourse> listClassCourse = studentCourseService.listClassCourse(classId);
        return JsonData.success(listClassCourse);
    }
    
    /**
     * 根据专业代码获取该专业所有教师课程
     * @param majorNo 专业代码
     * @param pageQuery 分页数据
     * @return JsonData
     */
    @RequestMapping("/listTeacherCourse")
    @ResponseBody
    public JsonData listTeacherCourse(@RequestParam("majorNo")String majorNo, PageQuery pageQuery) {
        PageInfo<TchCourseParam> listTchCourse = studentCourseService.listTchCourse(majorNo, pageQuery);
        return JsonData.success(listTchCourse);
    }
    
    /**
     * 单个或者批量添加课程给班级（实际上还是保存在stu_course表中）
     * @param tchCourseId 教师课程id数组
     * @param classId 班级id
     * @return JsonData
     */
    @RequestMapping("/addCourseToClass")
    @ResponseBody
    public JsonData addCourseToClass(@RequestParam("tchCourseId[]")int tchCourseId[],@RequestParam("classId")int classId) {
        if (tchCourseId.length >= 1) {
            // 添加
            studentCourseService.saveBatchStuCourse(tchCourseId, classId);
        } else {
            throw new ParamException("tchCourseId无参数");
        }
        return JsonData.success();
    }
    
    /**
     * 单个或者批量删除学生课程
     * @param courseId 学生课程id
     * @return JsonData
     */
    @RequestMapping("/delStuCourse")
    @ResponseBody
    public JsonData delStuCourse(@RequestParam("courseId[]")int courseId[]) {
        if (courseId.length >= 1) {
            studentCourseService.deleteStuCourse(courseId);
        } else {
            throw new ParamException("courseId无参数");
        }
        return JsonData.success();
    }
    
    // ============================================课表安排===============================================
    
    /**
     * 跳转到学生课表安排页面
     * @param classId 班级id
     * @param model
     * @return String manager/student/course/stuCourse_time.html
     */
    @RequestMapping("/toStuCourseTime")
    public String toStuCourseTime(@RequestParam("classId") int classId, Model model) {
        model.addAttribute("classId", classId);
        return "manager/student/course/stuCourse_time.html";
    }
    
    /**
     * 跳转到学生课表安排-编辑页面
     * @param courseId 学生课程id
     * @param model
     * @return String manager/student/course/stuCourse_time_edit.html
     */
    @RequestMapping("/toCourseTimeEdit")
    public String toCourseTimeEdit(@RequestParam("courseId") int courseId, Model model) {
        StuCourse stuCourse = studentCourseService.getStuCourse(courseId);
        model.addAttribute("stuCourse", stuCourse);
        return "manager/student/course/stuCourse_time_edit.html";
    }
        
    /**
     * 更新一条学生课程信息
     * @param stuCourse 学生课程信息
     * @return JsonData
     */
    @RequestMapping("/updateStuCourse")
    @ResponseBody
    public JsonData updateStuCourse(StuCourse stuCourse) {
        studentCourseService.updateStuCourse(stuCourse);
        return JsonData.success();
    }
    
    
    /**
     * 给学生课程自动排课
     * @param tchCourseIds 教师课程id数组
     * @return JsonData
     */
	/*
	 * @RequestMapping("/autoStuCourse")
	 * 
	 * @ResponseBody public JsonData autoStuCourse(@RequestParam("stuCourseIds[]")
	 * int stuCourseIds[]) { studentCourseService.autoStuCourse(stuCourseIds);
	 * return JsonData.success(); }
	 */
}
